package com.example.administrator.myapplication.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.databinding.ActivityLoginBinding;
import com.example.administrator.myapplication.until.BaseApplication;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private ActivityLoginBinding binding;
    private BaseApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        application = ((BaseApplication) getApplicationContext());
        aboutListener();
    }

    private void aboutListener() {
        binding.loginBack.setOnClickListener(this);
        binding.loginQq.setOnClickListener(this);
        binding.loginWeixin.setOnClickListener(this);
        binding.loginWeibo .setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_back:
                finish();
                break;
            case R.id.login_qq:
              login(new QQ(this));
                finish();
                break;
            case R.id.login_weixin:
              login(new Wechat(this));
                break;
            case R.id.login_weibo:
              login(new SinaWeibo(this));
                break;
        }
    }
    public void login(Platform platform) {
        // String userId = platform.getDb().getUserId();
        //Log.d("1620", userId + "userId");
//        if (userId != null) {
//            Toast.makeText(this, "已经登录过", Toast.LENGTH_SHORT).show();
//        } else {
        platform.setPlatformActionListener(new PlatformActionListener() {
            //   Platform.ACTION_AUTHORIZING: 1 要功能不要数据
            // Platform.ACTION_USER_INFOR : 2 要数据不要功能
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d("1620", "当前线程" + Thread.currentThread().getName());
                switch (i) {
                    case Platform.ACTION_AUTHORIZING:
                        Log.d("1620", "onComplete:第三方登录成功，要功能不要数据");
                        break;
                    case Platform.ACTION_USER_INFOR:
                        String nickname = (String) hashMap.get("nickname");
                        String figureurl_qq_1 = (String) hashMap.get("figureurl_qq_2");
                        Set<Map.Entry<String, Object>> entries = hashMap.entrySet();
                        for (Map.Entry<String, Object> entriy : entries) {
                            String key = entriy.getKey();
                            Object value = entriy.getValue();
                            Log.d("1620", "onComplete:第三方登录成功，要数据不要功能," + "key:" + key + ",value" + value);
                        }
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        intent.putExtra("nickName",nickname);
                        intent.putExtra("img",figureurl_qq_1);
                        startActivity(intent);
                        break;
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.d("1620", "登录错误");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d("1620", "登录取消");
            }
        });

        //  platform.authorize();  // 要功能不要数据
        platform.showUser(null);   //要数据不要功能
    }
}
